#include <wild/WildConfig.h>
#include <wild/WildArray.h>
#include <wild/SceneRoleMgr.h>
#include <wild/SceneRoleEntity.h>
#include <wild/PlatformMgr.h>


namespace wild
{


SceneRoleMgr::SceneRoleMgr(const char* pcName)
: _poEnties(NULL)
{
	_sScName = pcName;
	_poEnties = new ENTITY_LIST();
}

//-----------------------------------------------------------------------------------------------------
SceneRoleMgr::~SceneRoleMgr()
{
	SAFE_DEL(_poEnties);
	
}

//-----------------------------------------------------------------------------------------------------
void SceneRoleMgr::Start()
{
	this->OnInitialize();
}

//-----------------------------------------------------------------------------------------------------
void SceneRoleMgr::Stop()
{
	this->OnFinialize();

	_poEnties->Clear();


}

//-----------------------------------------------------------------------------------------------------
int SceneRoleMgr::AddEntity(SceneRoleEntity* poEnt,const char* pcName)
{
	if(!pcName||strlen(pcName)==0||!poEnt)
	{
		return IWA_NULL;
	}
	//poEnt->ComponentProc(T3D_EM_SETOWNER,0,(char*)_poSceneMgr);
	//poEnt->ComponentProc(T3D_EM_SETRENDERSYSTEM,0,(char*)_pRenderSystem);


	return _poEnties->Add(poEnt,pcName);
}

//-----------------------------------------------------------------------------------------------------
void SceneRoleMgr::DelEntity(const char* pcName)
{
	SceneRoleEntity* poEnt_ = _poEnties->GetObjectByName(pcName);

	if(poEnt_)
	{
		poEnt_->ComponentProc(T3D_EM_SHUTDOWN,0,0);
		poEnt_->ComponentProc(T3D_EM_DESTROY,0,0);

		_poEnties->Remove(pcName);
	}
}

//-----------------------------------------------------------------------------------------------------
void SceneRoleMgr::DelEntity(int iHandle)
{
	SceneRoleEntity* poEnt_ = _poEnties->GetObjectByID(iHandle);

	if(poEnt_)
	{
		poEnt_->ComponentProc(T3D_EM_SHUTDOWN,0,0);
		poEnt_->ComponentProc(T3D_EM_DESTROY,0,0);
		_poEnties->Remove(iHandle);
	}
	
}

//-----------------------------------------------------------------------------------------------------
SceneRoleEntity* SceneRoleMgr::GetEntity(const char* pcName)
{
	return _poEnties->GetObjectByName(pcName);
}

//-----------------------------------------------------------------------------------------------------
SceneRoleEntity* SceneRoleMgr::GetEntity(int iHandle)
{
	return _poEnties->GetObjectByID(iHandle);
}

//-----------------------------------------------------------------------------------------------------
int SceneRoleMgr::SendMessageToEntity(int iID,Uint uiMsg,int iParam1,char* pcParam2)
{
	SceneRoleEntity* poEnt_ = _poEnties->GetObjectByID(iID);

	if(poEnt_)
	{
		return poEnt_->ComponentProc(uiMsg,0,0);	
	}

	return 0;
}





}	//	namespace wild
